package tictactoe.screens;

import java.awt.*;
import java.awt.Font.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import tictactoe.*;
import tictactoe.types.*;

/**
 *
 * @author Ryan Falls
 */
public class MainMenuScreen extends MenuScreen
{
    //Constants
    protected final int borderSize = 8;
    //Components
    protected JLabel title;
    //TODO: Implement or remove
    /*
    protected JLabel newGameButton;
    protected JLabel playerXLabel;
    protected JLabel playerOLabel;
    protected JComboBox playerXSelection;
    protected JComboBox playerOSelection;
     */
    protected JLabel singlePlayerButton;
    protected JLabel selectDifficultyLabel;
    protected JLabel easyCPUButton;
    protected JLabel hardCPUButton;
    protected JLabel multiPlayerButton;
    protected JLabel freePlayButton;
    protected JLabel optionsButton;
    protected JLabel exitButton;
    protected JLabel backButton;
    protected JLabel resumeButton;

    //TODO Main Menu Screen
    public MainMenuScreen(TicTacToe ttt)
    {
	this.ttt = ttt;
	setLayout(new GridBagLayout());

	GridBagConstraints constraints = new GridBagConstraints();

	//Add Title
	title = new JLabel("Main Menu");
	title.setFont(ttt.menuTitleFont);
	constraints.gridx = 0;
	constraints.gridy = 0;
	constraints.gridheight = 1;
	constraints.gridwidth = 3;
	constraints.insets = new Insets(10, 10, 5, 10);
	add(title, constraints);

	//Configure Menu Item Properties
	constraints.gridwidth = 2;
	constraints.anchor = GridBagConstraints.WEST;
	constraints.insets.top = 5;

	//Add Single Player Button
	singlePlayerButton = new JLabel("Single Player");
	singlePlayerButton.setBorder(ttt.emptyBorder);
	singlePlayerButton.setFont(ttt.menuItemFont);
	constraints.gridy = 2;
	singlePlayerButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ShowDifficulties(true);
	    }
	});
	add(singlePlayerButton, constraints);

	//Add Difficulty Label
	selectDifficultyLabel = new JLabel("Select Difficulty:");
	selectDifficultyLabel.setFont(ttt.menuItemFont);
	constraints.gridy = 2;
	selectDifficultyLabel.setVisible(false);
	add(selectDifficultyLabel,constraints);

	//Add Easy CPU Button
	easyCPUButton = new JLabel("   Easy   ");
	easyCPUButton.setBorder(ttt.emptyBorder);
	easyCPUButton.setFont(ttt.menuItemFont);
	constraints.gridy = 3;
	easyCPUButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ChangeScreen(Screens.gameScreen);
		m.ShowDifficulties(false);
		m.ttt.Game.NewGame(GameType.Easy);
	    }
	});
	easyCPUButton.setVisible(false);
	add(easyCPUButton, constraints);

	//Add Easy CPU Button
	hardCPUButton = new JLabel("   Hard   ");
	hardCPUButton.setBorder(ttt.emptyBorder);
	hardCPUButton.setFont(ttt.menuItemFont);
	constraints.gridy = 4;
	hardCPUButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ChangeScreen(Screens.gameScreen);
		m.ShowDifficulties(false);
		m.ttt.Game.NewGame(GameType.Hard);
	    }
	});
	hardCPUButton.setVisible(false);
	add(hardCPUButton, constraints);

	//Add Multi Player Button
	multiPlayerButton = new JLabel("Multi Player");
	multiPlayerButton.setBorder(ttt.emptyBorder);
	multiPlayerButton.setFont(ttt.menuItemFont);
	constraints.gridy = 3;
	multiPlayerButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ChangeScreen(Screens.gameScreen);
		m.ShowDifficulties(false);
		m.ttt.Game.NewGame(GameType.Multi);
	    }
	});
	add(multiPlayerButton, constraints);

	//Add Free Play Button
	freePlayButton = new JLabel("Free Play");
	freePlayButton.setBorder(ttt.emptyBorder);
	freePlayButton.setFont(ttt.menuItemFont);
	constraints.gridy = 4;
	freePlayButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ChangeScreen(Screens.gameScreen);
		m.ShowDifficulties(false);
		m.ttt.Game.NewGame(GameType.Free);
	    }
	});
	add(freePlayButton, constraints);

	//Add Options Button
	optionsButton = new JLabel("Options");
	optionsButton.setBorder(ttt.emptyBorder);
	optionsButton.setFont(ttt.menuItemFont);
	constraints.gridy = 5;
	optionsButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());

		m.ChangeScreen(Screens.optionsMenu);
	    }
	});
	add(optionsButton, constraints);

	//Add Exit Button
	exitButton = new JLabel("Exit");
	exitButton.setBorder(ttt.emptyBorder);
	exitButton.setFont(ttt.menuItemFont);
	constraints.gridy = 6;
	constraints.insets.bottom = 10;
	exitButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		TicTacToe.AskExit();
	    }
	});
	add(exitButton, constraints);

	//Add Back Button
	backButton = new JLabel("Back");
	backButton.setBorder(ttt.emptyBorder);
	backButton.setFont(ttt.menuItemFont);
	constraints.gridy = 5;
	backButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ShowDifficulties(false);
	    }
	});
	backButton.setVisible(false);
	add(backButton, constraints);

	//Add Resume Button
	resumeButton = new JLabel("Resume");
	resumeButton.setBorder(ttt.emptyBorder);
	resumeButton.setFont(ttt.menuItemFont);
	constraints.gridy = 4;
	resumeButton.addMouseListener(new MenuItemMouseAdapter()
	{
	    @Override public void mouseClicked(MouseEvent e)
	    {
		Component c = e.getComponent();
		MainMenuScreen m = (MainMenuScreen) (c.getParent());
		m.ChangeScreen(Screens.gameScreen);
		m.ttt.Game.RedrawBoard();
	    }
	});
	resumeButton.setVisible(false);
	add(resumeButton, constraints);
    }

    /**
     * Used to show difficulties instead of regular menu items.
     * @param show Variable that indicates whether difficulties or regular
     * items should be shown.
     */
    private void ShowDifficulties(boolean show)
    {
	singlePlayerButton.setVisible(!show);
	selectDifficultyLabel.setVisible(show);

	easyCPUButton.setVisible(show);
	hardCPUButton.setVisible(show);
	multiPlayerButton.setVisible(!show);
	freePlayButton.setVisible(!show);

	optionsButton.setVisible(!show);

	exitButton.setVisible(!show);
	backButton.setVisible(show);
    }

    /**
     * Used to set whether the Main Menu should display the alternate options (default false).
     * Display the alternate menu when there is a currently active game.
     * @param alt   Set to true to display the alternate menu, false for default menu.
     * @see tictactoe.TTTHelpers#GetGameState() GetGameState
     */
    // Sets SinglePlayer, MultiPlayer, and Free Player buttons not visible and the
    //resume play button visible when there is a currently active game, and vice-versa.
    public void SetAlternate(boolean alt)
    {
	singlePlayerButton.setVisible(!alt);
	multiPlayerButton.setVisible(!alt);
	freePlayButton.setVisible(!alt);

	resumeButton.setVisible(alt);
    }
}
